
/*
 * C/C++ Lessons Program
 * 
 * Copyright (c) 2012 Ken Zhang (cpp.china@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <string.h>
#include <dirent.h>

int qsort_strcmp(const void *p1, const void *p2)
{
	return strcasecmp(*(const char **)p1, *(const char **)p2);
}

int main()
{
	DIR *dirp = opendir(".");
	if (dirp == NULL) {
		perror("Failed to open current directory");
		return -1;
	}

	const int MAX_FILES = 1024;
	char *names[MAX_FILES];
	int n = 0, i;
	struct dirent *entry;

	while ((entry = readdir(dirp)) != NULL && n < MAX_FILES) {
		names[n++] = entry->d_name;
	}
	closedir(dirp);

	qsort(names, n, sizeof(char *), qsort_strcmp);
	for (i = 0; i < n; i++) {
		printf("%s\n", names[i]);
	}

	return 0;
}

